package com.dlc.apk.utils

import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonElement
import com.google.gson.JsonParser
import java.lang.reflect.Type
import java.util.*

class GsonUtil private constructor() {
    private var mGSon = GsonBuilder().disableHtmlEscaping().create()
    fun parseObjToJsonStr(srcObj: Any?): String {
        var result = ""
        if (mGSon == null) {
            mGSon = GsonBuilder().disableHtmlEscaping().create()
        }
        try {
            result = mGSon!!.toJson(srcObj)
        } catch (var4: Exception) {
            Log.getStackTraceString(var4)
        }
        return result
    }

    fun <T> parseJsonStrToObj(src: String?, classOfT: Class<T>?): T? {
        return if (TextUtils.isEmpty(src)) {
            null
        } else {
            var result: T? = null
            if (mGSon == null) {
                mGSon = GsonBuilder().disableHtmlEscaping().create()
            }
            try {
                result = mGSon!!.fromJson(src, classOfT)
            } catch (var5: Exception) {
                Log.getStackTraceString(var5)
            }
            result
        }
    }

    fun <T> parseJsonElementToObj(src: JsonElement?, superClass: Type?): T? {
        var result: T? = null
        if (mGSon == null) {
            mGSon = GsonBuilder().disableHtmlEscaping().create()
        }
        try {
            result = mGSon!!.fromJson(src, superClass)
        } catch (var5: Exception) {
            Log.getStackTraceString(var5)
        }
        return result
    }

    fun <T> parseJsonStrToObj(json: String?, typeOfT: Type?): T? {
        var result: T? = null
        if (mGSon == null) {
            mGSon = GsonBuilder().disableHtmlEscaping().create()
        }
        try {
            result = mGSon!!.fromJson(json, typeOfT)
        } catch (var5: Exception) {
            Log.getStackTraceString(var5)
        }
        return result
    }

    fun <T> parseJsonToList(jsonData: JsonElement, cls: Class<T>?): List<T>? {
        val list: MutableList<T> = ArrayList<T>()
        val gson = Gson()
        val jsonArray = jsonData.asJsonArray
        val it: Iterator<*> = jsonArray.iterator()
        while (it.hasNext()) {
            val jsonElement = it.next() as JsonElement
            val json = jsonElement.toString()
            val bean = gson.fromJson(json, cls)
            list.add(bean)
        }
        return if (list != null && list.size != 0) list else null
    }

    @Throws(Exception::class)
    fun <T> fromJsonArray(json: String?, clazz: Class<T>?): List<T?> {
        val lst: MutableList<T> = ArrayList<T>()
        val array = JsonParser().parse(json).asJsonArray
        val var5: Iterator<*> = array.iterator()
        while (var5.hasNext()) {
            val elem = var5.next() as JsonElement
            lst.add(mGSon!!.fromJson(elem, clazz))
        }
        return lst
    }

    companion object {
        private var gInstance: GsonUtil? = null

        @JvmStatic
        @get:Synchronized
        val instance: GsonUtil
            get() {
                if (gInstance == null) {
                    gInstance = GsonUtil()
                }
                return gInstance!!
            }
    }
}